/*
 * Copyright (c) 2023-2299 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.task;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lam.common.constant.MallStateConstant;
import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.NumberUtil;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.Order;
import com.lam.mall.entity.OrderItem;
import com.lam.mall.entity.OrderSettlement;
import com.lam.mall.enums.PayStatus;
import com.lam.mall.service.OrderService;
import com.lam.mall.service.OrderSettlementService;
import com.lam.mall.service.PayService;
import com.lam.mall.service.ProdSkuService;
import com.lam.mall.service.ProductService;
import com.lam.schedule.common.scheduler.annotation.TaskTarget;

/**
 */
@TaskTarget
@Component
public class OrderTask {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProdSkuService prodSkuService;
    @Autowired
    private OrderSettlementService orderSettlementService;
    @Autowired
    private PayService payService;

    @TaskTarget
    public void cancelOrder(Map<String, Object> param){
    	
        // 获取超时未支付的订单
        List<Order> orders = orderService.listTimeoutOrder();
        if (CollectionUtil.isEmpty(orders)) {
            return;
        }
        
        for (Order order : orders) {
        	orderService.cancelOrder(order, MallStateConstant.ORDER_OPER_TYPE_SYS);
            for (OrderItem orderItem : order.getOrderItems()) {
                productService.removeProductCacheById(orderItem.getProdId());
                prodSkuService.removeCacheBySkuId(orderItem.getSkuId(),orderItem.getProdId());
            }
		}
    }
    
    @TaskTarget
    public void confirmPayResult(Map<String, Object> param){
    	
        // 获取未确认支付结果的订单
        List<Order> orders = orderService.listUnConfirmPayOrder();
        if (CollectionUtil.isEmpty(orders)) {
            return;
        }
        
        for (Order order : orders) {
        	
        	OrderSettlement orderSettlement = orderSettlementService.selectByOrderNumber(order.getOrderNumber());
        	if(orderSettlement == null) {
        		logger.error("订单支付结果确认时检查到订单结算信息不存在，orderNo:{}", order.getOrderNumber());
        		continue;
        	}
        	if(NumberUtil.eq(orderSettlement.getPayStatus(), PayStatus.PAYED.value()) || StringUtils.isBlank(orderSettlement.getPayNo()) 
        			|| StringUtils.isBlank(orderSettlement.getBizPayNo())) {
        		continue;
        	}
        	payService.queryPayResult(orderSettlement.getPayNo(), orderSettlement.getBizPayNo(), orderSettlement.getPayType());
		}
    }

    /**
     * 确认收货
     */
    @TaskTarget
    public void confirmOrder(Map<String, Object> param){
    	
        logger.info("系统自动确认收货订单...");
        // 获取15天之前已支付的订单
        List<Order> orders = orderService.listWaitConfirmOrder(-15);
        if (CollectionUtil.isEmpty(orders)) {
            return;
        }
        
        for (Order order : orders) {
        	orderService.confirmOrder(order, MallStateConstant.ORDER_OPER_TYPE_SYS);
            for (OrderItem orderItem : order.getOrderItems()) {
                productService.removeProductCacheById(orderItem.getProdId());
                prodSkuService.removeCacheBySkuId(orderItem.getSkuId(),orderItem.getProdId());
            }
        }
    }

}
